package com.yunchen.server.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.yunchen.server.util.Return;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试认证功能的控制器
 */
@RestController
@RequestMapping("/test-auth")
@CrossOrigin
public class TestAuthController {

    /**
     * 测试需要登录的接口
     */
    @GetMapping("/protected")
    public Return protectedEndpoint() {
        Return re = new Return();
        re.setCode(200);
        re.setMsg("访问成功，当前用户ID: " + StpUtil.getLoginIdAsString());
        return re;
    }

    /**
     * 测试token信息
     */
    @GetMapping("/token-info")
    public Return tokenInfo() {
        Return re = new Return();
        try {
            if (StpUtil.isLogin()) {
                re.setCode(200);
                re.setMsg("Token有效");
                re.setData("用户ID: " + StpUtil.getLoginIdAsString() + 
                          ", Token值: " + StpUtil.getTokenValue());
            } else {
                re.setCode(401);
                re.setMsg("未登录");
            }
        } catch (Exception e) {
            re.setCode(401);
            re.setMsg("Token无效或已过期");
        }
        return re;
    }
}
